import React from "react";
import { useSelector } from "react-redux";
import { Redirect, Route } from "react-router-dom";
import { USER_TOKEN_NAME } from "../config";

export function PrivateRoute(props: any) {
  const { Component, render: children, ...rest } = props;

  const isAuth =
    useSelector(
      (state: { authStore: any }) => state.authStore.isUserLoggedIn
    ) || localStorage.getItem(USER_TOKEN_NAME);

  // || false;

  return isAuth ? (
    <Route {...rest} render={(routeProps) => <Component {...routeProps} />} />
  ) : (
    <Redirect
      to={{
        pathname: "/login",
        state: {
          from: props.location.pathname,
        },
      }}
    />
  );
}
